package cn.tedu.ivos3.geofence.service.impl;


import cn.tedu.ivos3.base.exception.ServiceException;
import cn.tedu.ivos3.base.response.StatusCode;
import cn.tedu.ivos3.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos3.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos3.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos3.geofence.pojo.entity.Geofence;
import cn.tedu.ivos3.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos3.geofence.service.GeofenceService;
import cn.tedu.ivos3.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos3.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos3.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos3.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏业务:{}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        /* 之前没有对围栏的车辆数据进行查询，此处需要进行业务补充： */
        for (int i=0;i<list.size();i++){
            GeofenceVO geofenceVO = list.get(i);
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            int totalNum = vehicleVOList.size();
            int availableNum = 0;
            for (VehicleVO vehicleVO : vehicleVOList){
                if (vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
        log.debug("保存围栏信息业务参数：{}", geofenceSaveParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam,geofence);
        if (geofence.getId()==null){//新增
            geofence.setCreateTime(new Date());
            geofence.setStatus("1");//围栏默认启用
            geofenceMapper.insert(geofence);
        }else{//编辑
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }

    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除围栏业务参数：{}",id);
        //1.封装车辆的查询条件
        VehicleQuery vehicleQuery = new VehicleQuery();
        //2.向查询条件中设置当前围栏id
        vehicleQuery.setGeofenceId(id);
        //3.直接调用车辆mapper层查车
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //如果该围栏下有车，则不能删除
        if (list.size()>0 && list != null){
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        }else{
            geofenceMapper.deleteGeofence(id);
        }
    }
}
